package com.magicborrow.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.magicborrow.Constants;
import com.magicborrow.R;
import com.magicborrow.beans.AddressBean;
import com.magicborrow.beans.BaseBean;
import com.magicborrow.beans.StringBean;
import com.magicborrow.beans.WantBorringListBean;
import com.magicborrow.enums.PriceModeEnum;
import com.magicborrow.utils.UserTools;
import com.magicborrow.utils.ViewPressedUtil;
import com.magicborrow.utils.VolleyTool;
import com.magicborrow.views.MsgDialog;
import com.magicborrow.views.SelectorTimeDialog;
import com.magicborrow.views.SuccessDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class WantBorrowingActivity extends BaseActivity implements View.OnClickListener, VolleyTool.HTTPListener {

    private TextView tvTime;
    private TextView tvAddress;
    private AddressBean address;
    private int timesMode;
    private String times;
    private WantBorringListBean.DataEntity.WantBorrowingBean bean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_want_borrowing);
        //

        findViewById(R.id.btn_keeps).setOnClickListener(this);
        tvTime = (TextView) findViewById(R.id.tv_time);
        tvAddress = (TextView) findViewById(R.id.et_address);
        tvTime.setOnClickListener(this);
        findViewById(R.id.btn_push).setOnClickListener(this);
        findViewById(R.id.btn_keeps).setOnTouchListener(ViewPressedUtil.TouchPress);
        findViewById(R.id.btn_push).setOnTouchListener(ViewPressedUtil.TouchPress);
        tvAddress.setOnClickListener(this);
        initData();


    }

    private void initData() {
        Intent intent = getIntent();
        getEtPhone().setText(UserTools.getUser(this).getMobile());
        if (intent.hasExtra("data")) {
            bean = (WantBorringListBean.DataEntity.WantBorrowingBean) intent.getSerializableExtra("data");
            getEtPhone().setText(bean.getPhone());
            getEtStuffInfo().setText(bean.getDescription());
            getEtStuffName().setText(bean.getName());
            tvAddress.setText(bean.getAddress());
            tvTime.setText(bean.getTimes() + PriceModeEnum.getModeStr(bean.getTimesMode()));
            TextView tvTitle = (TextView) findViewById(R.id.tv_title);
            tvTitle.setText("编辑求借物品");
            times = "" + bean.getTimes();
            timesMode = bean.getTimesMode();
            address = new AddressBean();
            address.setAddress(bean.getAddress());
            address.setLat(bean.getLat());
            address.setLon(bean.getLon());
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.et_address:
                Intent intent = new Intent(this, AddressManagerActivity.class);
                startActivityForResult(intent, 2);
                break;
            case R.id.btn_keeps:
                pushData(0);
              /*  SuccessDialog sDialog = new SuccessDialog(this, "保存至草稿箱", R.mipmap.keeps_sucess);
                sDialog.show();

                sDialog.setOnViewPostsClick(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });*/

                //TODO implement
                break;
            case R.id.tv_time:
               /* Intent intent = new Intent(this,TestActivity.class);
                startActivity(intent);*/
                List<String> data2 = new ArrayList<>();
                data2.add("小时");
                data2.add("天");
                data2.add("周");
                final SelectorTimeDialog dialog = new SelectorTimeDialog(this, data2);
                dialog.show();
                dialog.setOnOkClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        timesMode = PriceModeEnum.getModeId(dialog.getCurrentPriceMode());
                        times = dialog.getTimeNumber();
                        tvTime.setText(times + dialog.getCurrentPriceMode());

                        dialog.dismiss();
                    }
                });
                //TODO implement
                break;
            case R.id.btn_push:
                pushData(1);

                //TODO implement
                break;
        }

    }

    private void pushData(int code) {
        if (!UserTools.isLogin(this, true))
            return;
        String name = getEtStuffName().getText().toString();
        if (TextUtils.isEmpty(name)) {
            getEtStuffName().setError("请输入商品名称");
            return;
        }
        String description = getEtStuffInfo().getText().toString();
        if (TextUtils.isEmpty(description)) {
            getEtStuffInfo().setError("请输入商品描述");
            return;
        }

   /*     String address = tvAddress.getText().toString();
        String addressDetail = this.address.getAddressInfo();*/
        String phone = getEtPhone().getText().toString();
        if (TextUtils.isEmpty(phone) || phone.length() != 11) {
            getEtPhone().setError("请输入正确的手机号");
            return;
        }
        if (TextUtils.isEmpty(times) || "0".equals(times)) {
            MsgDialog msgDialog = new MsgDialog(this, "请选择您要租借时长", null);
            msgDialog.show();
            return;
        }
        if (address == null) {
            MsgDialog msgDialog = new MsgDialog(this, "请选择您的地址", null);
            msgDialog.show();
            return;
        }


        HashMap<String, String> params = new HashMap<>();
        params.put("acc_token", UserTools.getUser(this).getAcc_token());
        params.put("name", "" + name);
        params.put("description", "" + description);
        params.put("address", "" + this.address.getAddress());
        params.put("addressDetail", "" + this.address.getAddressInfo());
        params.put("phone", "" + phone);
        params.put("box", "" + code);
        params.put("lon", "" + address.getLon());
        params.put("lat", "" + address.getLat());
        params.put("times", "" + times);
        params.put("timesMode", "" + timesMode);
        String url = Constants.URL_POST_PUSH_BORROWING;
        if (bean != null) {
            params.put("id", "" + bean.getId());
            url = Constants.URL_POST_UPDATE_BORROWING;
        }
        VolleyTool.post(url, params, this, code, StringBean.class);
    }

    private EditText getEtStuffName() {
        return (EditText) findViewById(R.id.et_stuff_name);
    }

    private EditText getEtStuffInfo() {
        return (EditText) findViewById(R.id.et_stuff_info);
    }

    private EditText getEtPhone() {
        return (EditText) findViewById(R.id.et_phone);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 2) {
                address = (AddressBean) data.getSerializableExtra("address");
                tvAddress.setText(address.getAddress() + "\n" + address.getAddressInfo());
                return;
            }
        }
    }

    @Override
    public <T> void onResponse(T t, int requestCode) {
        BaseBean bean = (BaseBean) t;
        if (bean.getCode() >= 0) {
            String msg = "";
            if (requestCode == 0) {
                msg = "保存至草稿箱";
            } else {
                msg = "发布成功";
            }

            SuccessDialog successDialog = new SuccessDialog(this, msg, R.mipmap.push_sucess);
            successDialog.show();

            successDialog.setOnViewPostsClick(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();

                }
            });
        } else {
            showShortMsg(bean.getMessage());
        }
    }

    @Override
    public void onErrorResponse(VolleyError error, int requestCode) {

    }
}
